package com.example.producer;

import com.example.common.Constant;
import com.example.common.RandomUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BoardCreation {
    private static final int n = 3, N = 9;
    private static final int MAX_COUNT = Constant.INITIAL_COUNT;//初始填入数字的个数
    private boolean[][] set1;
    private boolean[][] set2;
    private boolean[][][] set3;

    public char[][] create() {
        char[][] board = new char[N][N];
        set1 = new boolean[N][N];
        set2 = new boolean[N][N];
        set3 = new boolean[n][n][N];
        for (int i = 0; i < N; i++) {
            Arrays.fill(board[i], '.');
        }
        int remainNumber = MAX_COUNT;
        for (int i = 0; i < N * N && remainNumber > 0; i++) {
            double random = Math.random();
            int remainBlank = N * N - i;
            if (random <= (double)remainNumber / remainBlank) {
                int x0 = i / N, y0 = i % N;//该空格所在的行、列的坐标信息
                List<Integer> validNumbers = new ArrayList<>();
                for (int j = 0; j < N; j++) {
                    if (set1[x0][j] || set2[y0][j] || set3[x0/3][y0/3][j]) {
                        continue;
                    }
                    validNumbers.add(j);//记录可以合法填入空格的数字
                }
                if (validNumbers.isEmpty()) {
                    continue;
                }
                int x = RandomUtils.getRandomInt(validNumbers.size());
                Integer j0 = validNumbers.get(x);
                char num = (char)('1' + j0);
                board[x0][y0] = num;
                remainNumber--;
                set1[x0][j0] = set2[y0][j0] = set3[x0/3][y0/3][j0] = true;
            }
        }
        return valid(board) ? board : null;
    }

    private boolean valid(char[][] board) {
        //9*3的矩形，不允许两行全为空；3*9的矩形，不允许两列全为空
        for (int k = 0; k < 3; k++) {
            int emptyRow = 0;
            for (int i = 3 * k; i < 3 * k + 3; i++) {
                int temp = 1;
                for (int j = 0; j < N; j++) {
                    if (board[i][j] != '.') {//不是空行
                        temp = 0;
                        break;
                    }
                }
                emptyRow += temp;
            }
            if (emptyRow >= 2) {
                System.out.println("9*3矩形有两行为空");
                return false;
            }
        }
        for (int k = 0; k < 3; k++) {
            int emptyRow = 0;
            for (int j = 3 * k; j < 3 * k + 3; j++) {
                int temp = 1;
                for (int i = 0; i < N; i++) {
                    if (board[i][j] != '.') {//不是空列
                        temp = 0;
                        break;
                    }
                }
                emptyRow += temp;
            }
            if (emptyRow >= 2) {
                System.out.println("3*9矩形有两列为空");
                return false;
            }
        }
        int emptySquare = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int temp = 1;
                for (int k = 0; k < N; k++) {
                    if (set3[i][j][k]) {
                        temp = 0;
                        break;
                    }
                }
                emptySquare += temp;
            }
        }
        if (emptySquare >= 5) {
            System.out.println("有5个及以上空的3*3小正方形");
            return false;
        }
        return true;
    }
}
